// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   LMSPackageHandler.java

package com.iphaeton.lms.course.scorm12.runtime.core;

import java.io.*;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.*;
import org.apache.log4j.Logger;

public class LMSPackageHandler
{

    public LMSPackageHandler()
    {
    }

    public static String version()
    {
        System.out.println("*************");
        System.out.println("in version()");
        System.out.println("*************\n");
        String versionId = new String("");
        versionId = "Version 1.03 For Live Site";
        return versionId;
    }

    public static void display(String zipFileName)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("*************");
            logger.debug("in display()");
            logger.debug("*************\n");
        }
        try
        {
            logger.debug("** " + zipFileName + " **\n");
            logger.debug("*****************************************");
            logger.debug("The Package Contains the following files:");
            logger.debug("*****************************************\n");
            zf = new ZipFile(zipFileName);
            for(Enumeration entries = zf.entries(); entries.hasMoreElements(); logger.debug(((ZipEntry)entries.nextElement()).getName()));
            zf.close();
        }
        catch(IOException e)
        {
            logger.debug("IO Exception Caught: " + e);
        }
        logger.debug("\n\n");
    }

    public static String extract(String zipFileName, String extractedFile, String pathOfExtract)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("***********************");
            logger.debug("in extract()           ");
            logger.debug("***********************");
            logger.debug("zip file: " + zipFileName);
            logger.debug("file to extract: " + extractedFile);
        }
        String nameOfExtractedFile = new String("");
        try
        {
            String pathAndName = new String("");
            ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
            int indexOfFileBeginning = extractedFile.lastIndexOf("/") + 1;
            nameOfExtractedFile = extractedFile.substring(indexOfFileBeginning);
            pathAndName = pathOfExtract + "\\" + nameOfExtractedFile;
            OutputStream out = new FileOutputStream(pathAndName);
            byte buf[] = new byte[1024];
            for(int flag = 0; flag != 1;)
            {
                ZipEntry entry = in.getNextEntry();
                logger.info(entry.getName());
                if(entry.getName().equalsIgnoreCase(extractedFile))
                {
                    logger.debug("Found file to extract...  extracting to " + pathOfExtract);
                    flag = 1;
                }
            }

            int len;
            while((len = in.read(buf)) > 0) 
                out.write(buf, 0, len);
            out.close();
            in.close();
        }
        catch(IOException e)
        {
            logger.debug("IO Exception Caught: " + e);
            e.printStackTrace();
        }
        return nameOfExtractedFile;
    }

    public static boolean findManifest(String zipFileName)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("***********************");
            logger.debug("in findManifest()      ");
            logger.debug("***********************\n");
        }
        boolean rtn = false;
        try
        {
            ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
            for(int flag = 0; flag != 1 && in.available() != 0;)
            {
                ZipEntry entry = in.getNextEntry();
                if(in.available() != 0 && entry.getName().equalsIgnoreCase("imsmanifest.xml"))
                {
                    logger.debug("Located manifest.... returning true");
                    flag = 1;
                    rtn = true;
                }
            }

            in.close();
        }
        catch(IOException e)
        {
            logger.debug("IO Exception Caught: " + e);
        }
        return rtn;
    }

    public static boolean findMetadata(String zipFileName)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("***********************");
            logger.debug("in findMetadata()      ");
            logger.debug("***********************\n");
        }
        boolean rtn = false;
        String suffix = ".xml";
        try
        {
            ZipInputStream in;
            for(in = new ZipInputStream(new FileInputStream(zipFileName)); in.available() != 0;)
            {
                ZipEntry entry = in.getNextEntry();
                if(in.available() != 0 && entry.getName().endsWith(suffix))
                {
                    rtn = true;
                    logger.debug("Other Meta-data located... returning true");
                }
            }

            in.close();
        }
        catch(IOException e)
        {
            logger.debug("IO Exception Caught: " + e);
        }
        return rtn;
    }

    public static Vector locateMetadata(String zipFileName)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("***********************");
            logger.debug("in locateMetadata()    ");
            logger.debug("***********************\n");
        }
        Vector metaDataVector = new Vector();
        String suffix = ".xml";
        try
        {
            ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
            logger.debug("Other meta-data located:");
            while(in.available() != 0) 
            {
                ZipEntry entry = in.getNextEntry();
                if(in.available() != 0 && entry.getName().endsWith(suffix))
                {
                    logger.debug(entry.getName());
                    metaDataVector.addElement(entry.getName());
                }
            }
            in.close();
        }
        catch(IOException e)
        {
            logger.debug("IO Exception Caught: " + e);
            e.printStackTrace();
        }
        return metaDataVector;
    }

    public static String getListOfMetadata(String zipFile)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("***********************");
            logger.debug("in getListOfMetadata() ");
            logger.debug("***********************\n");
        }
        Vector mdVector = new Vector();
        mdVector = locateMetadata(zipFile);
        String mdString = new String();
        mdString = mdVector.toString();
        if(logger.isDebugEnabled())
        {
            logger.debug("**********************************************");
            logger.debug("in getListOfMetadata(): String is " + mdString);
            logger.debug("**********************************************\n");
        }
        return mdString;
    }

    private static transient Logger logger;
    public static ZipFile zf;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.course.scorm12.runtime.core.LMSPackageHandler.class.getName());
    }
}
